layui.use(['table', 'admin', 'ax', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    /**
     * Goods Management<!-- 商品表管理 -->
     */
    var BasicGoods = {
        tableId: "basicGoodsTable"
    };

    /**
     * Initialize table columns<!-- 初始化表格的列 -->
     */
    BasicGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {width: 120,align: 'center', toolbar: '#tableBar', title: 'Actions'},
            {field: 'goodsCustomerName', title: 'Customer'},
            {field: 'goodsName', title: 'Goods Name'},
            {field: 'goodsNumber', title: 'Number'},
            {field: 'goodsNo', title: 'Product No'},
            {field: 'goodsHelpcode', title: 'Help Code'},
            {field: 'goodsUnit', title: 'Unit'},
            {field: 'goodsHeavybubble', title: 'Heavy/Bulky'},
            {field: 'goodsPacking', title: 'Packing'},
            {field: 'goodsSpecs', title: 'Specifications'},
            {field: 'goodsBigtype', title: 'Category'},
            {field: 'goodsType', title: 'Type'},
            {field: 'goodsBottom', title: 'Bottom'},
            {field: 'goodsLamination', title: 'Lamination'},
            {field: 'goodsTraylayer', title: 'Tray Layer'},
            {field: 'goodsSupportvolume', title: 'Capacity per Tray'},
            {field: 'goodsVolume', title: 'Volume m³'},
            {field: 'goodsWeight', title: 'Weight Kg'},
            {field: 'goodsCharging', title: 'Billing Price'},
            {field: 'goodsWarranty', title: 'Shelf Life (days)'},
            {field: 'xianggui', title: 'Carton Specs'},
            {field: 'goodsNodes', title: 'Remarks'},
            {field: 'createTime', title: 'Create Time'},
        ]];
    };

    /**
     * Click search button<!-- 点击查询按钮 -->
     */
    BasicGoods.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['minPrice'] = $("#minPrice").val();
        queryData['maxPrice'] = $("#maxPrice").val();
        queryData['goodsBigtype'] = $("#goodsBigtype").val();
        queryData['goodsType'] = $("#goodsType").val();
        queryData['goodsCustomerId'] = $("#goodsCustomerId").val();
        table.reload(BasicGoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // Import
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: 'Goods Import',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/basicGoods/toimportExcel',
            end: function () {
                table.reload(BasicGoods.tableId);
            }
        });
    });

    // Import Update
    $('#btnExpimportUpdate').click(function () {
        layer.open({
            type: 2,
            title: 'Goods Update Import',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/basicGoods/toimportExcelUpdate',
            end: function () {
                table.reload(BasicGoods.tableId);
            }
        });
    });

	//Pull Shanding new goods information<!-- 拉取商鼎新商品信息 -->
    $('#pullSd').click(function () {
		submit = 2
        layer.open({
            type: 2,
            title: 'Pull Shangding New Goods',
            area: ['850px', '600px'],
            anim: 5,
            content: Feng.ctxPath + '/basicGoods/topullSd',
            end: function () {
				if(submit == 1){
        			table.reload(BasicGoods.tableId);
				}
            }
        });
    });

    /**
     * Pop up add dialog<!-- 弹出添加对话框 -->
     */
    BasicGoods.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicGoods/add';
    };

    /**
     * Export Excel button<!-- 导出excel按钮 -->
     */
    BasicGoods.exportExcel = function () {
        var checkRows = table.checkStatus(BasicGoods.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export"); // Please select data to export<!-- 请选择要导出的数据 -->
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * Click Edit<!-- 点击编辑 -->
     *
     * @param data 点击按钮时候的行数据
     */
    BasicGoods.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicGoods/edit?id=' + data.id;
    };

    /**
     * Click Delete<!-- 点击删除 -->
     *
     * @param data 点击按钮时候的行数据
     */
    BasicGoods.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicGoods/delete", function (data) {
                Feng.success("Delete successful!"); // Delete successful<!-- 删除成功! -->
                table.reload(BasicGoods.tableId);
            }, function (data) {
                Feng.error("Delete failed!" + data.responseJSON.message + "!"); // Delete failed<!-- 删除失败! -->
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Confirm deletion?", operation); // Confirm deletion<!-- 是否删除? -->
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicGoods.tableId,
        url: Feng.ctxPath + '/basicGoods/list',
        page: true,
        limit: 60,
		limits:[60,120,240,360,480,1500,5000],
        height: "full-60",
        cellMinWidth: 100,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: BasicGoods.initColumn()
    });

    $('#btnPrint').click(function () {
    	var checkStatus = table.checkStatus('basicGoodsTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].goodsNumber);
		}
	        layer.open({
	            type: 2,
	            title: 'Batch Print Location Codes',
	            area: ['900px', '550px'],
	            anim: 5,
	            content: Feng.ctxPath + '/basicGoods/tobtnqrCode?ids='+ids,
	            end: function () {
	            }
	        });
	} else {
		layer.alert('Please select at least one item'); // Please select at least one item<!-- 请至少选中一条数据 -->
	}
    });

    // 渲染时间选择框--保质期
    laydate.render({
        elem: '#goodsWarranty'
    });

    // Search button click event<!-- 搜索按钮点击事件 -->
    $('#btnSearch').click(function () {
        BasicGoods.search();
    });

    // Add button click event<!-- 添加按钮点击事件 -->
    $('#btnAdd').click(function () {
        BasicGoods.openAddDlg();
    });

    // Export Excel<!-- 导出excel -->
    $('#btnExp').click(function () {
        BasicGoods.exportExcel();
    });

    // Export Excel by specification<!-- 导出excel -->
    $('#btnExpBySpec').click(function () {
        BasicGoods.exportExcelBySpec();
    });

    /**
     * Export Excel by specification button<!-- 按规格导出excel按钮 -->
     */
    BasicGoods.exportExcelBySpec = function () {
		window.location.href = Feng.ctxPath + '/basicGoods/exportExcelBySpec?goodsCustomerId='+$("#goodsCustomerId").val()+"&condition2="+$("#condition").val();
    };

    // 工具条点击事件
    table.on('tool(' + BasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicGoods.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicGoods.onDeleteItem(data);
        }
    });
});
